var express = require('express');
var router = express.Router();
const dayjs=require('dayjs');
const AccountModel = require('../modules/account');



router.get('/', (req, res, next)=>{
    res.redirect('/account')
});
/* GET home page. */
router.get('/account', function(req, res, next) {
  console.log(req.cookies)
  if(req.cookies.name){
    AccountModel.find().sort({date:-1}).then(response=>{
    res.render('list',{list:response,dayjs:dayjs})
  })
  }else{
    res.redirect('/login')
  }  
  })

// })
;
router.post('/account', function(req, res, next) {
    const newdate=dayjs(req.body.date).format()
   const data={...req.body,date:newdate}
   AccountModel.create({
    ...data
   }).then(res=>{
    console.log(res)
   }).catch(err=>{
    console.log(err.message)
   })
  res.render('success',{url:'/account',msg:'添加成功'})
});
router.get('/account/add', function(req, res, next) {
  res.render('item')
});



module.exports = router;
